package com.hequ.merchant.banner.handler;

import com.hequ.merchant.banner.entity.Paging;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 戴文龙 on 2014/12/29.
 */
public class PageJsonResponseHandler extends JsonResponseHandler<Paging> {

    private static final String JSON_KEY_RECORDS = "records";
    private static final String JSON_KEY_PAGE_SIZE = "size";
    private static final String JSON_KEY_PAGE_COUNT = "count";
    private static final String JSON_KEY_CURRENT_PAGE = "current";

    @Override
    protected Paging parseItem(JSONObject json) throws JSONException {
        Paging paging = new Paging();

        if (hasKeyValue(json, JSON_KEY_RECORDS)) {
            paging.setRecords(json.getLong(JSON_KEY_RECORDS));
        }

        if(hasKeyValue(json,JSON_KEY_PAGE_COUNT)) {
            paging.setPageCount(json.getInt(JSON_KEY_PAGE_COUNT));
        }

        if(hasKeyValue(json,JSON_KEY_CURRENT_PAGE)){
            paging.setCurrentPage(json.getInt(JSON_KEY_CURRENT_PAGE));
        }

        if(hasKeyValue(json,JSON_KEY_PAGE_SIZE)) {
            paging.setPageSize(json.getInt(JSON_KEY_PAGE_SIZE));
        }

        return paging;
    }
}
